package com.tddx.activiti.utils.ftl.data;

import java.io.File;
import java.util.List;

import com.tddx.activiti.business.core.dao.GentiDao;
import com.tddx.activiti.business.core.entity.UserTabColsEntity;
import com.tddx.activiti.utils.SpringContextHolder;
import com.tddx.activiti.utils.StringUtils;
import com.tddx.activiti.utils.ftl.FtlUtil;

public class BaseData {

	private String tableName;
	
	private String name;

	private String path;

	private String entityName;
	
	private String controllerName;
	
	private String serviceName;
	
	private String daoName;
	
	private String jdbcName;
	
	private String minpckage;
	
	private String maxpckage;
	
	private Boolean tree;
	
	private List<UserTabColsEntity> colList;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		this.path = this.path.replace(File.separator, FtlUtil.fseperator);
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getEntityName() {
		this.entityName = this.name + "Entity";
		return entityName;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}

	public String getControllerName() {
		this.controllerName = this.name + "Ctrl";
		return controllerName;
	}

	public void setControllerName(String controllerName) {
		this.controllerName = controllerName;
	}

	public String getServiceName() {
		this.serviceName = this.name + "Service";
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getDaoName() {
		this.daoName = this.name + "Dao";
		return daoName;
	}

	public void setDaoName(String daoName) {
		this.daoName = daoName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getJdbcName() {
		this.jdbcName = this.name + "Jdbc";
		return jdbcName;
	}

	public void setJdbcName(String jdbcName) {
		this.jdbcName = jdbcName;
	}

	public String getMinpckage() {
		this.minpckage = path.substring(0, path.indexOf(FtlUtil.fseperator));
		return minpckage;
	}

	public void setMinpckage(String minpckage) {
		this.minpckage = minpckage;
	}

	public String getMaxpckage() {
		this.maxpckage = path.substring(path.indexOf(FtlUtil.fseperator) + 1);
		return maxpckage;
	}

	public void setMaxpckage(String maxpckage) {
		this.maxpckage = maxpckage;
	}

	public List<UserTabColsEntity> getColList() {
		return colList;
	}

	public void setColList(List<UserTabColsEntity> colList) {
		this.colList = colList;
	}

	public Boolean getTree() {
		return tree;
	}

	public void setTree(Boolean tree) {
		this.tree = tree;
	}

	/**
	 * 示例：BaseData ctrl = new BaseData("SYS_T_USER", "SysTuser", "sys/systuser");
	 * @param name 名称前缀
	 * @param path 访问路径
	 */
	public BaseData(String tableName, String name, String path) {
		this(tableName, name, path, false);
	}
	
	/**
	 * 示例：BaseData ctrl = new BaseData("SYS_T_USER", "SysTuser", "sys/systuser");
	 * @param name 名称前缀
	 * @param path 访问路径
	 */
	public BaseData(String tableName, String name, String path, Boolean tree) {
		super();
		this.tableName = tableName;
		this.name = name;
		this.path = path;
		this.tree = tree;
		this.colList = SpringContextHolder.getBean(GentiDao.class).findCols(tableName);
	}
	
	/**
	 * 根据表名获取BaseData实体类
	 * @param tableName 表名如：getInstance("SYS_T_USER");
	 * @return BaseData
	 */
	public static BaseData getInstance(String tableName, boolean tree){
		String lowertableName = StringUtils.lowerCase(tableName);
		String name = StringUtils.capitalizeBySymbol(lowertableName, "$", "_");
		String path = tableName.substring(0, tableName.indexOf("_"));
		BaseData data = new BaseData(tableName, name, StringUtils.lowerCase(path) + File.separator + StringUtils.lowerCase(name), tree);
		return data;
	}
	/**
	 * 根据表名获取BaseData实体类
	 * @param tableName 表名如：getInstance("SYS_T_USER");
	 * @return BaseData
	 */
	public static BaseData getInstance(String tableName){
		return getInstance(tableName, false);
	}

	@Override
	public String toString() {
		return "BaseData [name=" + name + ", path=" + path + ", entityName=" + entityName + ", controllerName="
				+ controllerName + ", serviceName=" + serviceName + ", daoName=" + daoName + ", minpckage=" + minpckage
				+ ", maxpckage=" + maxpckage + "]";
	}

}
